<?php

    /** 
    * utils_mail.php - main email sending 
    * 
    * @package     Heurist academic knowledge management system
    * @link        http://HeuristNetwork.org
    * @copyright   (C) 2005-2014 University of Sydney
    * @author      Artem Osmakov   <artem.osmakov@sydney.edu.au>
    * @license     http://www.gnu.org/licenses/gpl-3.0.txt GNU License 3.0
    * @version     4.0
    */

    /*
    * Licensed under the GNU License, Version 3.0 (the "License"); you may not use this file except in compliance
    * with the License. You may obtain a copy of the License at http://www.gnu.org/licenses/gpl-3.0.txt
    * Unless required by applicable law or agreed to in writing, software distributed under the License is
    * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied
    * See the License for the specific language governing permissions and limitations under the License.
    */

    
    function sendEmail($email_to, $email_title, $email_text, $email_header, $is_utf8=false){

        $res = null;

        if(!$email_to){
            $res = "Mail send failed. Recipient email address is not defined.";
        }else if(!$email_text){
            $res = "Mail send failed. Message text is not defined.";
        }

        if(!$res){

            if(!$email_title){
                $email_title = "";
            }
            $email_title = "[HEURIST] ".$email_title;

            if(!$email_header){
                $email_header = "From: HEURIST";
                if(defined('HEURIST_SERVER_NAME')){
                    $email_header = $email_header." (".HEURIST_DBNAME.") <no-reply@".HEURIST_SERVER_NAME.">";
                }
            }


            if($is_utf8){
                $email_header = $email_header."\r\nContent-Type: text/plain; charset=utf-8\r\n";
                $email_title = '=?utf-8?B?'.base64_encode($email_title).'?=';
            }

            $email_text = $email_text."\n\n"
            ."-------------------------------------------\n"
            ."This email was generated by Heurist"
            .(defined('HEURIST_BASE_URL') ?(":\n".HEURIST_BASE_URL) :"")."\n";


            $rv = mail($email_to, $email_title, $email_text, $email_header);
            if(!$rv){
                $res = "Can not send email. This may indicate that mail transport agent is not correctly configured on server. Please advise the system adminstrator";
            }
        }

        if($res){ //error
            error_log($res);
        }else{
            $res = "ok";        
        }

        return "ok";
    }  
?>
